// 1.工程化
import Vue from 'vue'
import vueRouter from 'vue-router'
Vue.use(vueRouter);
// 2.创建路由对象并配置路由
const router = new vueRouter({
    routes: [
        {
            path: '/',
            redirect: { name: 'index' }
        },
        {
            name: 'login',
            path: '/login',
            component: () => import('@/views/login.vue')
        },
        {
            name: 'register',
            path: '/register',
            component: () => import('@/views/register.vue')
        },

        {
            name: 'personal',
            path: '/personal/:id',
            component: () => import('@/views/personal.vue')
        },
        {
            name: 'editProfile',
            path: '/editProfile/:id',
            component: () => import('@/views/editProfile.vue')
        },
        {
            name: 'index',
            path: '/index',
            component: () => import('@/views/index.vue')
        },
        {
            name: 'postDetail',
            path: '/postDetail/:id',
            component: () => import('@/views/postDetail.vue'),
            props: true
        },
        {
            name:'myfollows',
            path:'/myfollows',
            component: () => import('@/views/myfollows.vue'),
        },
        {
            name: 'mystars',
            path: '/mystars',
            component: () => import('@/views/mystars.vue')
        },
        {
            name: 'comment',
            path: '/comment/:id',
            component: () => import('@/views/comment.vue')
        },
        {
            name:'cateManager',
            path:'/cateManager',
            component: () => import('@/views/cateManager.vue')
        },
        {
            name:'search',
            path:'/search',
            component: () => import('@/views/search.vue')
        }
    ]
})
// 设置一个黑名单中,这里面的路由名称都需要验证是否需要登录
const arr = ['personal', 'editProfile','cateManager']
// 不通过登陆也能直接跳转到个人中心页所以添加导航守卫
router.beforeEach((to, from, next) => {
    // to 实际就是单页面的this.$route
    // console.log(to); 
    if (arr.indexOf(to.name) !== -1) {
        let token = localStorage.getItem('toutiaoToken')
        if (token) {
            next()
        } else {
            next({ name: 'login' })
        }
    } else {
        next()
    }
})
// 3.暴露
export default router